package com.dream.blog.controller;

import com.dream.blog.entity.UserInfo;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by spring on 2018/9/27.
 */
//@Controller 负责注册一个bean 到spring 上下文中
@Controller
public class LoginController {

    //@RequestMapping 注解为控制器指定可以处理哪些 URL 请求
    //@ResponseBody该注解用于将Controller的方法返回的对象，通过适当的HttpMessageConverter转换为指定格式后，
    // 写入到Response对象的body数据区
    @RequestMapping("/login")
    @ResponseBody
    public Object login(){
        return "Hello Word!";
    }
    //自动匹配参数
    @RequestMapping("/out")
    @ResponseBody
    public String  outLogin(String name,String password){
        System.out.printf("name"+name+"password"+password);
        return "OUT";
    }
    //自动装箱
    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(UserInfo userInfo){
        System.out.printf("Name"+userInfo.getName()+"Password"+userInfo.getPassword());
        return "ADD";
    }
    //使用InitBinder处理Date类型
    @RequestMapping("/getDate")
    @ResponseBody
    public String date(Date date){
        System.out.printf("日期"+date);
        return String.valueOf(date);
    }

    @InitBinder
    public void  initBinder(ServletRequestDataBinder binder){
        binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }

    //文件上传
    @RequestMapping(value = "/fileUpload",method = RequestMethod.POST)
    public String upload(HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest mreq = (MultipartHttpServletRequest) request;
        MultipartFile file = mreq.getFile("file");
        String fileName= file.getOriginalFilename();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        FileOutputStream fos = new FileOutputStream(request.getSession().getServletContext().getRealPath("/")+"upload/"+sdf.format(new Date())
                +fileName.substring(fileName.lastIndexOf(".")));
        fos.write(file.getBytes());
        fos.flush();
        fos.close();
        return "OK";
    }

    //Restful风格
    @RequestMapping(value = "/login/{id}",method = RequestMethod.GET)
    public String get(@PathVariable("id") Integer id){
        System.out.printf("get"+id);
        return ""+id;
    }
    @RequestMapping(value = "/login/{id}",method = RequestMethod.POST)
    public String POST(@PathVariable("id") Integer id){
        System.out.printf("POST"+id);
        return ""+id;
    }
    @RequestMapping(value = "/login/{id}",method = RequestMethod.PUT)
    public String PUT(@PathVariable("id") Integer id){
        System.out.printf("PUT"+id);
        return ""+id;
    }
    @RequestMapping(value = "/login/{id}",method = RequestMethod.DELETE)
    public String DELETE(@PathVariable(value = "/login/{id}")Integer id){
        System.out.printf("DELETE"+id);
        return ""+id;
    }

}
